/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.xuexiang.xui_lib.component.textview.supertextview.adapter;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

/**
 * 仅提供get方法的Map的包装类, get时如果不包含key则抛出异常, key自动忽略大小写
 *
 * @param <V> 值泛型
 * @since 2021-04-09
 */
public class ContainsMap<V> {
    private final Map<String, V> map;
    private final V defValue;

    private ContainsMap(Map<String, V> map, V defValue) {
        this.map = map;
        this.defValue = defValue;
    }

    /**
     * 返回key对应的value, 如果key为空则返回设置的默认值
     *
     * @param key key
     * @return 对应的value, 或为defValue
     */
    public V get(String key) {
        return get(key, defValue);
    }

    /**
     * 返回key对应的value, 如果key为空则返回defValue
     *
     * @param key key
     * @param def key为空值时的默认值
     * @return 对应的value, 或为defValue
     * @throws IllegalArgumentException 如果没找Key对应的值返回异常
     */
    public V get(String key, V def) {
        if (key == null) {
            return def;
        }
        String upperKey = key.toUpperCase(Locale.ROOT);
        if (map.containsKey(upperKey)) {
            return map.get(upperKey);
        }
        throw new IllegalArgumentException(
            "No found the" + key + "! the key must in [" + map.entrySet().toString() + "]");
    }

    /**
     * MapBuilder
     *
     * @param <V> 值类型
     * @since 2021-04-09
     */
    public static class Builder<V> {
        private final HashMap<String, V> map;
        private final V defValue;

        /**
         * 构造器
         *
         * @param initialCapacity 初始大小,设置合理的大小有利于节省内存控件
         * @param defValue 默认值
         */
        public Builder(int initialCapacity, V defValue) {
            this.map = new HashMap<>(initialCapacity);
            this.defValue = defValue;
        }

        /**
         * 构造器
         *
         * @param initialCapacity 初始大小,设置合理的大小有利于节省内存控件
         */
        public Builder(int initialCapacity) {
            this.map = new HashMap<>(initialCapacity);
            this.defValue = null;
        }

        /**
         * 填充key-value
         *
         * @param key 键值key
         * @param value 值
         * @return Builder对象
         */
        public Builder<V> put(String key, V value) {
            map.put(key.toUpperCase(Locale.ROOT), value);
            return this;
        }

        /**
         * 生成Map对象
         *
         * @return 设置的ContainsMap 对象
         */
        public ContainsMap<V> build() {
            return new ContainsMap<>(map, defValue);
        }
    }
}
